/*
 * @Author: Luo jiaxing 
 * @Date: 2017-05-22 14:35:18 
 * @Last Modified by: cgd
 * @Last Modified time: 2017-10-31 10:41:07
 * 新建方案Controller
 */
Ext.define('Admin.view.solutionManage.NewSolutionController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.newSolutionController',
    views: ['Admin.view.solutionManage.newSolutions.Year',
        'Admin.view.solutionManage.newSolutions.Month',
        'Admin.view.solutionManage.newSolutions.Xun'],
    showGHT: function (dom) {
        // var domid = dom.el.id + '-body';
        // $('#' + domid).html('<div style="width: 100%;height: 100%;margin: auto;' +
        // 'background:url(\'resources/images/fh/ght2.jpg\') no-repeat;background-size:100% 100%;"></div>');
    },
    getDateStr: function (date) {
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var month2 = month >= 10 ? month : '0' + month;
        var day = date.getDate();
        var day2 = day >= 10 ? day : '0' + day;
        return year + '-' + month2 + '-' +day2 + " 00:00:00";
    },
    validateInput: function () {
        var solutionName = this.lookupReference('proNm').getValue();
        var startDate = this.lookupReference('bgDt').getValue();
        var endDate = this.lookupReference('edDt').getValue();
        var producer = this.lookupReference('producer').getValue();
        var byr = this.lookupReference('byr').getValue();
        return solutionName && startDate && endDate && producer && byr;
    },
    toYearSolution: function (id, action) {
        var self = this;
        var type = '1';
        var msg = "确定新建方案?";
        // if (!self.validateInput()) {
        //     Ext.Msg.alert("提示信息", "请完善方案信息!");
        //     return;
        // }
        Ext.Msg.confirm("确认", msg, function (res) {
            if (res == "yes") {
                $.when(self.saveNewSolution(type)).then(function () {
                    var yearTab = Ext.getCmp('yearSolutionTab');
                    yearTab.add({
                        xtype: 'yearDispatch'
                    });
                    yearTab.setActiveItem(1);
                });
            }
        });
    },
    toMonthSolution: function () {
        var self = this;
        var type = '2';
        var msg = "确定新建方案?";
        // if (!self.validateInput()) {
        //     Ext.Msg.alert("提示信息", "请完善方案信息!");
        //     return;
        // }
        Ext.Msg.confirm("确认", msg, function (res) {
            if (res == "yes") {
                $.when(self.saveNewSolution(type)).then(function () {
                    var monthTab = Ext.getCmp('monthSolutionTab');
                    monthTab.add({
                        xtype: 'monthDispatch'
                    });
                    monthTab.setActiveItem(1);
                });
            }
        });
    },
    toXunSolution: function () {
        var self = this;
        var type = '3';
        var msg = "确定新建方案?";
        // if (!self.validateInput()) {
        //     Ext.Msg.alert("提示信息", "请完善方案信息!");
        //     return;
        // }
        Ext.Msg.confirm("确认", msg, function (res) {
            if (res == "yes") {
                $.when(self.saveNewSolution(type)).then(function () {
                    var xunTab = Ext.getCmp('xunSolutionTab');
                    xunTab.add({
                        xtype: 'xunDispatch'
                    });
                    xunTab.setActiveItem(1);
                });
            }
        });
    },
    saveNewSolution: function (type) {
        var def = $.Deferred();
        var self = this;
        var beginDate = self.lookupReference('bgDt').getValue();
        var crDt = self.getDateStr(new Date());
        var newSolution = {
            proTp: type,
            subCd: config.RIVER,
            proNm: self.lookupReference('proNm').getValue(),
            rvCd: config.RIVER,
            yr: beginDate.getFullYear(),
            byr: self.lookupReference('byr').getValue(),
            dpCyc: type,
            producer: self.lookupReference('producer').getValue(),
            crDt: crDt,
            pubSta: 0,
            bgDt: self.getDateStr(beginDate),
            edDt: self.getDateStr(self.lookupReference('edDt').getValue()),
            ts: crDt,
            nt: self.lookupReference('nt').getValue(),
        };
        if (type == '2' || type == '3') {
            newSolution.dpplCd = self.lookupReference('dpplCd').getValue();
        }
        $.ajax({
            url: '/jsite-szy-dispatch/a/szy/dispatch/ddsRdP/save',
            method: 'POST',
            contentType: 'application/json',
            // dataType: 'json',
            data: JSON.stringify(newSolution),
            success: function (response) {
                var proCd = response.id;
                if (response.code === 1) {
                    if (type == '1') {
                        $.dispatch.setYearProCd(proCd);
                    } else if (type == '2') {
                        $.dispatch.setMonthProCd(proCd);
                    } else if (type == '3') {
                        $.dispatch.setXunProCd(proCd);
                    }
                    def.resolve();
                } else {
                    Ext.Msg.alert("提示信息", "新建方案失败，" + response.msg);
                }
            }
        })
        return def.promise();
    }
})
